<template>
	<view>
		<!-- 主体 -->
		<view class="j_banner">
			<view class="j_item">
				<img src="http://127.0.0.1:7001/public/user/caidan.png" alt="">
			</view>
		</view>

		<view class="j_forms">
			<u-form :model="form" ref="uForm">
				<!-- 昵称 -->
				<u-form-item prop="nickname">
					<u-input v-model="form.nickname" placeholder="请输入昵称" />
				</u-form-item>
				<!-- 手机号 -->
				<u-form-item prop="username">
					<u-input v-model="form.username" placeholder="请输入手机号" />
				</u-form-item>
				<!-- 密码 -->
				<u-form-item prop="password">
					<u-input v-model="form.password" placeholder="请输入密码" />
				</u-form-item>
				<!-- 验证码 -->
				<u-form-item prop="Verification" class="Verification">
					<u-input v-model="form.Verification" placeholder="请输入验证码" />
					<!-- 获取验证码 -->
					<view class="j_Verification">
						<u-toast ref="uToast"></u-toast>
						<u-verification-code :seconds="seconds" @end="end" @start="start" ref="uCode"
							@change="codeChange"></u-verification-code>
						<text @tap="getCode">{{tips}}</text>
					</view>
				</u-form-item>
			</u-form>
			<u-button @click="submit" type="warning">注册</u-button>
		</view>
		<!-- 消息弹出提示 -->
		<view>
			<u-toast ref="uToast" />
		</view>
		<!-- 底部协议 -->
		<view class="j_bottom">
			<text>同意</text>
			<text class="j_service" @click="agreement">用户协议</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//验证码值
				tips: '',
				// refCode: null,时间
				seconds: 10,
				form: {
					//昵称
					nickname: '',
					//手机号
					username: '',
					//密码
					password: '',
					//验证码
					Verification: '',
				},
				//表单校验规则
				rules: {
					nickname: [{
						required: true,
						message: '请输入昵称',
						trigger: ['change', 'blur'],
					}],
					username: [{
							min: 6,
							message: '请输入正确手机号',
							trigger: 'change'
						},
						{
							validator: (rule, value, callback) => {

								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							trigger: ['change', 'blur'],
						}
					],
					//密码验证
					password: [{
						required: true,
						min: 6,
						max: 12,
						message: '密码长度在6-12之间',
						trigger: 'change'

					}],
					//验证码校验
					Verification: [{
						required: true,
						len: 4,
						message: '请输入验证码',
						trigger: 'change'
					}]
				}
			};
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			//用户协议
			agreement(){
				uni.navigateTo({
					url:'/pages/login/agreement/agreement'
				})
			},
			submit() {
				console.log(this.form)
				if (this.form.Verification === this.yan) {
					this.$refs.uForm.validate(valid => {
						if (valid) {
							//提示注册成功
							this.$refs.uToast.show({
									title: '注册成功',
									type: 'success',
									url: '/pages/login/login'
								}),
								//向数据库添加数据
								this.$api.postuser({
									username: this.form.username,
									password: this.form.password,
									nickname: this.form.nickname
								}).then(res => {
									// console.log(res)
								})
						}
					});
				} else {
					this.$refs.uToast.show({
						title: '验证码错误',
					})
				}

			},
			//验证码
			huoyan() {
				this.yan =
					`${Math.floor(Math.random()*10)}${Math.floor(Math.random()*10)}${Math.floor(Math.random()*10)}${Math.floor(Math.random()*10)}`
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					this.huoyan()
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码' + this.yan);
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 1000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
		},

	};
</script>

<style lang="scss" scoped>
	//主体
	.j_banner {
		width: 100%;
		height: 400upx;
		padding: 100upx 100upx 0upx 280upx;
		box-sizing: border-box;

		.j_item {
			width: 200upx;
			height: 200upx;

			img {
				width: 100%;
			}
		}
	}

	//表单验证
	.j_forms {
		padding: 0 20upx 0 20upx;
		box-sizing: border-box;

		//获取验证码
		.Verification {
			position: relative;

			//获取验证码
			.j_Verification {
				width: 200upx;
				height: 50upx;
				border: 2upx solid #000;
				position: absolute;
				top: 30upx;
				right: 10upx;
				text-align: center;
				line-height: 50upx;
				box-sizing: border-box;
			}
		}
	}

	//底部协议
	.j_bottom {
		width: 100%;
		height: 50upx;
		margin-top: 30upx;
		padding: 0upx 40upx 0 40upx;
		line-height: 50upx;

		.j_service {
			color: #FCBA69;
			font-size: 30upx;
			margin-left: 10upx;
		}
	}
</style>
